import Vue from 'vue'
window.formatDate = function(now) {
    var time = new Date(now)
    var y = time.getFullYear() //年
    var m = time.getMonth() + 1 //月
    var d = time.getDate() //日
    var h = time.getHours() //时
    var mm = time.getMinutes() //分
    var s = time.getSeconds() //秒
    return y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
}
window.setCookie = function(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + 60333333)
    document.cookie = c_name + '=' + escape(value) +
        ((expiredays == null) ? '' : ';expires=' + exdate.toGMTString())
}

// 获取cookie
window.getCookie = function(name) {
    var arr,
        reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
    if ((arr = document.cookie.match(reg))) return unescape(arr[2])
    else return null
}

window.objcookieB = function(uri, rk, dn, ty) {
        var objcookie = unescape(getCookie('objdetailData'))
        objcookie = JSON.parse(objcookie)
            //console.log(objcookie);
        var pigData = { 'objectUri': uri, 'rowKey': rk, 'displayName': dn, 'type': ty }

        var MapJy = []
        $(objcookie).each(function(i, n) {
            console.log('aa:' + n)
            MapJy.push(n.rowKey)
        })
        console.log('bb:' + MapJy)
        if (objcookie == null) {
            var objcookie = []
            objcookie.push(pigData)
            setCookie('objdetailData', JSON.stringify(objcookie))
            Vue.prototype.$message.success('添加成功')
        } else {
            if ($.inArray(rk, MapJy) == -1) {
                objcookie.push(pigData)
                setCookie('objdetailData', JSON.stringify(objcookie))
                Vue.prototype.$message.success('添加成功')

            } else {
                Vue.prototype.$message.error('已添加')
                return false
            }
        }
    }
    // 删除cookie
window.delCookie = function(name) {
    var exp = new Date()
    exp.setTime(exp.getTime() - 1)
    var cval = getCookie(name)
    if (cval != null) document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
}
window.timestampToTime = function(timestamp) {
        var date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-'
        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        D = date.getDate() + ' '
        h = date.getHours() + ':'
        m = date.getMinutes() + ':'
        s = date.getSeconds()
        return Y + M + D + h + m + s
    }
    // 去重
Array.prototype.distinct = function() {
    var arr = this,
        len = arr.length
    arr.sort(function(a, b) { // 对数组进行排序才能方便比较
        return a - b
    })

    function loop(index) {
        if (index >= 1) {
            if (arr[index] === arr[index - 1]) {
                arr.splice(index, 1)
            }
            loop(index - 1) //递归loop函数进行去重
        }
    }
    loop(len - 1)
    return arr
};
// 图片转base64
window.getBase64Image = function(url) {
    var canvas = document.createElement('canvas') //创建canvas DOM元素
    var ctx = canvas.getContext('2d')
    var img = new Image
    img.crossOrigin = 'Anonymous'
    img.src = url
    var ext = url.slice(url.lastIndexOf('.') + 1)
    img.onload = function() {
        canvas.height = 60 //指定画板的高度,自定义
        canvas.width = 85 //指定画板的宽度，自定义
        ctx.drawImage(img, 0, 0, 60, 85) //参数可自定义
        var dataURL = canvas.toDataURL('image/' + ext)
        canvas = null
        return dataURL
    };
}